import {Component} from '@angular/core';
import {IonicPage, ModalController, NavController, NavParams} from 'ionic-angular';
import {HttpProvider} from "@providers/http";
import {DialogProvider} from "@providers/dialog";

/**
 * 单据搜索界面
 */

@IonicPage()
@Component({
  selector: 'page-manager-order-search',
  templateUrl: 'manager-order-search.html',
})
export class ManagerOrderSearchPage {
  searchText: any = '';//搜索关键词
  YYSJ: any = {};//预约时间
  DJLY: any[];//单据来源
  DDZT: any[];//订单状态
  DJLX:any[];//单据类型
  DJLYisClicked = false;//单据来源是否收起
  DDZTisClicked = false;//订单状态是否收起
  DJLXisClicked = false;//单据类型是否收起

  status = this.navParams.get('status')||{
    KEY:'DPG',
      VALUE:'待派工'
  };

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private modalCtrl: ModalController,
              private http: HttpProvider,
              private dialog: DialogProvider) {
  }

  ionViewDidLoad() {
    this.getDDZT();
    this.getDJLY();
    this.getDJLX();
  }

  ionViewWillEnter(){

  }

  //搜索点击事件
  search() {
    let djly = this.DJLY.filter(value=>{
      return value.isSelected;
    });
    let ddzt = this.DDZT.filter(value=>{
      return value.isSelected;
    });
    let djlx = this.DJLX.filter(value=>{
      return value.isSelected;
    });
    this.navCtrl.push('ManagerOrderPage', {
      'isSearching':true,
      'searchText': this.searchText.trim(),
      'YYSJ': this.YYSJ,
      'DJLY': djly,
      'DDZT': ddzt,
      'DJLX': djlx,
      'status':this.status
    });
  }

  //选择预约时间
  openYYSJCalendar() {
    let myCalendar = this.modalCtrl.create('DatePickerPage');
    myCalendar.present();
    myCalendar.onDidDismiss(date => {
      if (date) {
        if (date == true) {
          this.YYSJ = {};
        } else {
          this.YYSJ = date
        }

      }
    });
  }

  //获取单据来源类型
  getDJLY() {
    this.http.post("/Inbound/invoke.do", {
      interfaceId: "csm.css.jcdy.qcfwquery.gzlx",
      data: JSON.stringify({'TYPE': 'css_ddly'})
    }).subscribe(message => {
      this.DJLY = message.data;
      this.DJLY.forEach(value => {
        value.isSelected = false;
      })
    }, error => {
      this.dialog.showAlert('请检查网络！' + error);
    });
  }

  //获取订单状态类型
  getDDZT() {
    this.http.post("/Inbound/invoke.do", {
      interfaceId: "csm.css.jcdy.qcfwquery.djzt",
      data: JSON.stringify({'GDLC': this.status.KEY})
    }).subscribe(message => {
      this.DDZT = message.data;
      this.DDZT.forEach(value => {
        value.isSelected = false;
      })
    }, error => {
      this.dialog.showAlert('请检查网络！' + error);
    });
  }

  //获取单据类型
  getDJLX(){
    this.http.post("/Inbound/invoke.do", {
      interfaceId: "csm.css.jcdy.qcfwquery.fwlx",
      data: JSON.stringify({})
    }).subscribe(message => {
      this.DJLX = message.data;
      this.DJLX.forEach(value => {
        value.isSelected = false;
      })
    }, error => {
      this.dialog.showAlert('请检查网络！' + error);
    });
  }

}
